//
//  CheckerboardFirstViewController.swift
//  CustomTransitions
//
//  Created by fuyoufang on 2021/10/26.
//

import Foundation
import UIKit

class CheckerboardFirstViewController: BaseFirstViewController {
    
    let layer = CAShapeLayer()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "Checkerboard"
        self.navigationController?.delegate = self
        
        layer.frame = CGRect(x: 100, y: 100, width: 100, height: 100)
        view.layer.addSublayer(layer)
        layer.backgroundColor = UIColor.red.cgColor
    }
    
    override func showSecond(sender: Any) {
        let viewController = CheckerboardSecondViewController()
        navigationController?.pushViewController(viewController, animated: true)
    }
    
    var index = 1
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if index % 2 == 0 {
            layer.backgroundColor = UIColor.black.cgColor
            layer.frame = CGRect(x: 200, y: 200, width: 200, height: 200)
        } else {
            layer.backgroundColor = UIColor.red.cgColor
            layer.frame = CGRect(x: 100, y: 100, width: 100, height: 100)
        }
        index += 1
    }
}

extension CheckerboardFirstViewController: UINavigationControllerDelegate {
    func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationController.Operation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return CheckerboardTransitionAnimator()
    }
}
